import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_app7/common/net/address.dart';
import 'package:flutter_app7/common/net/help/data_helper.dart';
import 'package:flutter_app7/common/net/mynet/dio_manager.dart';
import 'package:flutter_app7/common/net/mynet/http_method.dart';
import 'package:flutter_app7/common/net/mynet/response_type.dart';
import 'package:flutter_app7/models/login.dart';
import 'package:flutter_app7/models/login_hint.dart';
import 'package:flutter_app7/models/push.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class LoginRoute extends StatefulWidget {
  @override
  _LoginRouteState createState() => _LoginRouteState();
}

class _LoginRouteState extends State<LoginRoute> {
  var leftRightPadding = 30.0;
  var topBottomPadding = 4.0;
  var textTips = new TextStyle(fontSize: 16.0, color: Colors.black);
  var hintTips = new TextStyle(fontSize: 15.0, color: Colors.black26);

  var _userPassController = new TextEditingController();
  var _userNameController = new TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return FlutterEasyLoading(
        child: Scaffold(
            resizeToAvoidBottomInset: true,
            appBar: AppBar(title: Text("登陆")),
            body: SingleChildScrollView(
                child: Column(
              mainAxisSize: MainAxisSize.max,
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                new Padding(
                  padding: new EdgeInsets.fromLTRB(
                      leftRightPadding, 50.0, leftRightPadding, 10.0),
                  child: new Image.asset(
                    'asset/images/test.png',
                    width: 200,
                    height: 200,
                  ),
                ),
                new Padding(
                  padding: new EdgeInsets.fromLTRB(
                    leftRightPadding,
                    20.0,
                    10,
                    topBottomPadding,
                  ),
                  child: new TextField(
                    style: hintTips,
                    controller: _userNameController,
                    maxLines: 1,
                    decoration: new InputDecoration(hintText: "请输入用户名"),
                    obscureText: true,
                  ),
                ),
                new Padding(
                  padding: new EdgeInsets.fromLTRB(
                    leftRightPadding,
                    20.0,
                    10,
                    topBottomPadding,
                  ),
                  child: new TextField(
                    style: hintTips,
                    maxLines: 1,
                    controller: _userPassController,
                    decoration: new InputDecoration(hintText: "请输入密码"),
                    obscureText: true,
                  ),
                ),
                new Container(
                  width: 360.0,
                  margin: new EdgeInsets.fromLTRB(10.0, 40.0, 10.0, 0.0),
                  padding: new EdgeInsets.fromLTRB(leftRightPadding,
                      topBottomPadding, leftRightPadding, topBottomPadding),
                  child: new Card(
                    color: Colors.green,
                    elevation: 6.0,
                    child: new FlatButton(
                        onPressed: () {
                          print("the pass is" +
                              _userPassController.text +
                              _userNameController.text);

                          //  _testRequest(_userPassController.text, _userNameController.text);
                          //test();
                          testOrder();
                        },
                        child: new Padding(
                          padding: new EdgeInsets.all(10.0),
                          child: new Text(
                            '马上登录',
                            style: new TextStyle(
                                color: Colors.white, fontSize: 16.0),
                          ),
                        )),
                  ),
                )
              ],
            ))));
  }
}

void testOrder() async {
  var params1 = DataHelper.getBaseParamsMap();
  params1["access_token"] = "G6VQeYE2n03GxnhbCar5oarjbFAuKFr7";
  params1["uid"] = "952776";

  DioManager().httpRequest(Method.POST, Address.order_api, params: params1,
      responseType:  HttpResponseType.Int,
      successInt: (msg, successData) {
    print("成功：success:" + successData.toString());
  }, error: (error) {
    //  直接取 错误码  和 错误信息 使用
    print(" 错误：error code = ${error.code}, massage = ${error.message}");
  });
}


void test() async {
  var params1 = DataHelper.getBaseParamsMap();
  params1["access_token"] = "G6VQeYE2n03GxnhbCar5oarjbFAuKFr7";
  params1["uid"] = "952776";

  DioManager().httpRequest(Method.GET, Address.push_api,
      params: params1,
      responseType: HttpResponseType.List, successList: (msg, successData) {
    //  解析 list
    List cardbeanList =
        successData.map((m) => new Autogenerated.fromJson(m)).toList();
    Autogenerated autogenerated = cardbeanList.first;
    print("成功：success: list：" + autogenerated.title);
    int size = cardbeanList.length;
    print("获取数据成功 长度：$size ");
  },

      success: (msg, successData) {
    print("成功：success:" + successData.toString());
  }, successInt: (msg, successData) {
    print("成功：success: Int:" + successData.toString());
  }, successString: (msg, successData) {
    print("成功：success: String :" + successData);
  }, successBool: (msg, successData) {
    print("成功：success: Bool :" + successData.toString());
  }, successNullData: (msg) {
    print("成功：success:" + msg);
  }, successResponse: (successData) {
    print("成功：success  successResponse 原数据:" + successData.data.toString());
  }, error: (error) {
    //  直接取 错误码  和 错误信息 使用
    print(" 错误：error code = ${error.code}, massage = ${error.message}");
    //   特殊情况 如果需要查看服务器 响应 的 全部信息 可以解析 response   获取想要结果
    if (error.response != null) {
      print(" 错误json: ${error.response.data.toString()}");
      LoginHint data = LoginHint.fromJson(error.response.data);
      print(" 错误json: ${data.msg}");
    }
  });
}

void _testRequest(name, pwd) async {
  var params1 = DataHelper.getBaseParamsMap();
  Map<String, String> headParams = {
    "head1": "headValue1",
    "head2": "headValue2",
    "head3": "headValue3",
  };
  bool iswithLoading = true;

  params1["userName"] = "13590277737";
  params1["passWord"] = "e10adc3949ba59abbe56e057f20f883e";

  // 返回 LoginEntity
  DioManager().httpRequest(Method.POST, Address.login_api,
      params: params1,
      head: headParams,
      withLoading: iswithLoading, success: (msg, successData) {
    LoginEntity loginEntity = LoginEntity.fromJson(successData);
    print("loginEntity:${loginEntity.shopName}");
  }, error: (error) {
    //  直接取 错误码  和 错误信息 使用
    print(" 错误：error code = ${error.code}, massage = ${error.message}");

    /* //   特殊情况 如果需要查看服务器 响应 的 全部信息 可以 response 解析出来
    if (error.response != null) {
      print(" 错误json: ${error.response.data.toString()}");
      LoginHint data = LoginHint.fromJson(error.response.data);
      print(" 错误json: ${data.msg}");
    }
    */
  });
}
